import uuid
from dataclasses import dataclass
from typing import Any

import pytest
from sqlalchemy import insert
from sqlalchemy.ext.asyncio import AsyncSession

from recruit.adapter.database.gateway import ResumePositionGateway
from recruit.adapter.database.models import Resume, ResumePosition
from .fixtures.resume import ResumeTest
from .utils import check_attr_model, delete_user, check_get_one, get_resume


@dataclass
class ResumePositionTest:
    id: uuid.uuid4()
    resume_id: uuid.uuid4()
    position: str
    employment: str
    work_schedule: str


async def _check_resume(
        session: AsyncSession,
        data: ResumePositionTest
) -> None:
    resume_position = await get_resume(session, ResumePosition, data.id)
    assert isinstance(resume_position, ResumePosition)
    check_get_one(
        resume_position,
        data,
        ('id', 'resume_id', 'position', 'employment', 'work_schedule')
    )


async def _create_resume_position(
        session: AsyncSession,
        data: dict[str, Any]
) -> ResumePositionTest:
    stmt = insert(ResumePosition).values(**data).returning(ResumePosition)
    resume_position = (await session.execute(stmt)).scalar_one()
    resume_position = ResumePositionTest(
        id=resume_position.id,
        resume_id=resume_position.resume_id,
        position=resume_position.position,
        employment=resume_position.employment,
        work_schedule=resume_position.work_schedule
    )
    await session.commit()
    return resume_position


async def test_create(
        db_session: AsyncSession,
        get_resume_position: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_position_gateway = ResumePositionGateway(db_session)
    get_resume_position['resume_id'] = create_resume.id
    resume_position = await resume_position_gateway.create(
        **get_resume_position
    )
    assert isinstance(resume_position.resume, Resume)
    check_attr_model(resume_position, get_resume_position)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)


@pytest.mark.parametrize(
    'keys', [
        ('id', 'resume_id', 'position', 'employment', 'work_schedule')
    ]
)
async def test_get_one(
        keys: tuple[str, ...],
        db_session: AsyncSession,
        get_resume_position: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_position_gateway = ResumePositionGateway(db_session)
    get_resume_position['resume_id'] = create_resume.id
    created_resume_position = await _create_resume_position(
        db_session,
        get_resume_position
    )
    for key, value in get_resume_position.items():
        resume_position = await resume_position_gateway.get_one(**{key: value})
        assert resume_position is not None
        check_get_one(resume_position, created_resume_position, keys)
    await delete_user(db_session, create_resume.user_id)


async def test_check_resume(
        db_session: AsyncSession,
        get_resume_position: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_position_gateway = ResumePositionGateway(db_session)
    get_resume_position['resume_id'] = create_resume.id
    created_resume_position = await _create_resume_position(
        db_session,
        get_resume_position
    )
    val = await resume_position_gateway.check_resume(
        created_resume_position.resume_id
    )
    assert val
    await delete_user(db_session, create_resume.user_id)
    val = await resume_position_gateway.check_resume(
        created_resume_position.resume_id
    )
    assert not val


async def test_delete(
        db_session: AsyncSession,
        get_resume_position: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_position_gateway = ResumePositionGateway(db_session)
    get_resume_position['resume_id'] = create_resume.id
    created_resume_position = await _create_resume_position(
        db_session,
        get_resume_position
    )
    await resume_position_gateway.delete(created_resume_position.id)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)
    resume_position = await get_resume(
        db_session,
        ResumePosition,
        created_resume_position.id
    )
    assert resume_position is None
